package com.weishaoying.thrift.learn.service;

import com.weishaoying.thrift.learn.entity.Car;
import com.weishaoying.thrift.learn.entity.User;
import org.apache.thrift.TException;

import java.util.*;

public class UserServiceImpl implements UserService.Iface {

    private Random random = new Random();

    @Override
    public void insert(com.weishaoying.thrift.learn.entity.User user) throws TException {
        System.out.println("----正在插入：user = [" + user + "]");
    }

    @Override
    public com.weishaoying.thrift.learn.entity.User getUserById(int id) throws TException {
        System.out.println("========正在调用 getUserById()");
//        int r;
//        if ((r = random.nextInt(10)) > 2) {
//            throw new RuntimeException("Oh No," + r);
//        }
//        try {
//            Thread.sleep(1500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        User user = new User();
        user.setId(id);
        user.setUsername("Lily");
        user.setCreateDate(new Date().getSeconds());
        user.setUpdateDate(System.currentTimeMillis());
        Car car = new Car();
        car.setBrand("Audi Q5");
        car.setColor("Black");
        car.setPrice(10000 + random.nextInt(10000));
        car.setBuyDate(System.currentTimeMillis());
        user.setCarList(new ArrayList<>(Arrays.asList(car)));
        return user;
    }

    @Override
    public List<User> getAllUsers() throws TException {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100 + i);
            user.setUsername("Sunny" + random.nextFloat());
            user.setCreateDate(random.nextLong());
            user.setUpdateDate(System.currentTimeMillis());
            Car car = new Car();
            car.setBrand("Audi Q7");
            car.setColor("Red");
            car.setPrice(10000 + random.nextInt(20000));
            car.setBuyDate(System.currentTimeMillis());
            user.setCarList(new ArrayList<>(Arrays.asList(car)));
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<com.weishaoying.thrift.learn.entity.User> pageQueryUser(int pageNo, int pageSize, String username) throws TException {
        System.out.println("----pageQueryUser().pageNo = [" + pageNo + "], pageSize = [" + pageSize + "], username = [" + username + "]");
        return null;
    }


}
